package com.danco.planner.essences;

import java.util.Date;

import com.danco.planner.essences.arraylists.LecturesList;
import com.danco.planner.essences.arraylists.StudentsList;
import com.danco.planner.sortings.compareby.*;

public class Course implements ICompareByDate,ICompareByStudentsCount,ICompareBySection,ICompareBySpeaker,ICompareByName {

	protected String name;
	public String getName() {
		return name;
	}
	
	protected String description;
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	protected SectionEnum section;
	public SectionEnum getSection() {
		return section;
	}

	protected Date startDate;
	public Date getStartDate() {
		return startDate;
	}
	
	protected Date finishDate;
	public Date getFinishDate() {
		return finishDate;
	}
	
	protected Speaker speaker;
	public Speaker getSpeaker() {
		return speaker;
	}
	
	protected StudentsList studentsList;
	public StudentsList getStudentsList() {
		return studentsList;
	}
	
	protected LecturesList lecturesList;
	public LecturesList getLecturesList() {
		return lecturesList;
	}
	
	
	public Course(String name, String description, SectionEnum section, Date sDate, Date eDate) {
		super();
		
		this.name = name;
		this.description = description;
		this.section = section;
		this.startDate = (sDate.getTime() <= eDate.getTime())?sDate:eDate;
		this.finishDate = (this.startDate.equals(sDate))?eDate:sDate;
		
		this.studentsList = new StudentsList();
		this.lecturesList = new LecturesList();
	}

	public boolean addLecture(Lecture lecture) {
		return lecturesList.add(lecture);
	}
	public boolean removeLecture(Lecture lecture) {
		return lecturesList.remove(lecture);
	}
	public void removeAllLectures() {
		lecturesList.clear();
	}
	
	public boolean addStudent(Student student) {
		return studentsList.add(student);
	}
	public boolean removeStudent(Student student) {
		return studentsList.remove(student);
	}
	public void removeAllStudents() {
		studentsList.clear();
	}
	
	public boolean addSpeaker(Speaker speaker) {
		
		if (this.speaker != null) {
			removeSpeaker();
		}
		
		this.speaker = speaker;
		return this.speaker.getCoursesList().add(this);
	}
	public void removeSpeaker() {
		this.speaker.getCoursesList().remove(this);
		this.speaker = null;
	}
	
	@Override 
	public String toString() {
		
		return String.format("Course: %1$s \n\t\tSpeaker: %2$s \n\t\tLectures: %3$d \n\t\tStudents: %4$d\n\t\tStart: %5$te/%5$tm/%5$tY \n\t\tFinish: %6$te/%6$tm/%6$tY" ,
				name,speaker.getName(),lecturesList.size(),studentsList.size(),startDate,finishDate);
	}
	public String toString(boolean withDescripton) {
		
		if (withDescripton) {
			return "Description: " + description + "; " + toString();
		} else {
			return toString();
		}
	}
	
	
	@Override
	public String getNameForCompare() {
		// TODO Auto-generated method stub
		return name;
	}
	@Override
	public Speaker getSpeakerForCompare() {
		// TODO Auto-generated method stub
		return speaker;
	}
	@Override
	public SectionEnum getSectionForCompare() {
		// TODO Auto-generated method stub
		return section;
	}
	@Override
	public Integer getStudentsCountForCompare() {
		// TODO Auto-generated method stub
		return studentsList.size();
	}
	@Override
	public Date getDateForCompare() {
		// TODO Auto-generated method stub
		return startDate;
	}
}
